package com.inray.nbs.mybatis;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Before;
import org.junit.Test;

import com.zst.cb.test.TableToEntityTool;


/**
 * 反向工程工具类
 * @author Administrator
 *
 */
public class TestDBTool {
	private String packageName="com.inray.nbs.bean";//将实体类生成在哪个包中
	private String packagePath="/com/inray/nbs/bean/";//实体类文件生成路径
	private int DBType=2;//1为mysql或SqlServer  2为oracle
	private String DBName="evs";//数据库名称
	private String DBDriver="oracle.jdbc.driver.OracleDriver";//数据库驱动
	private String DBUrl="jdbc:oracle:thin:@139.196.164.147:1521:ORCL";
	private String username="evs";
	private String pw="evs";
	private TableToEntityTool t=null;
	//是否生成mybatis的映射文件(com.zst.cb.mappers包)，注意：生成的文件会覆盖之前的文件,根据需要来决定是否生成。
	private boolean isMapper=true;
	private String mapperPath="/com/inray/nbs/mappers/";//生成映射文件存放的路径
	@Before
	public void init(){
		System.out.println("gg");
		t = new TableToEntityTool(packageName, packagePath, DBType, DBName, DBDriver, DBUrl, username, pw,isMapper,mapperPath);
		System.out.println("gg22");
	}
	public static void main(String arg[]){
		TestDBTool tb=new TestDBTool();
		tb.init();
		try {
			tb.tableToEntityAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 会将数据库中所有的表都生成实体类和映射文件
	 * @throws SQLException
	 */
	//@Test
	public void tableToEntityAll() throws SQLException{
		Connection conn = t.getConnection();
		DatabaseMetaData meta = conn.getMetaData();
		ResultSet rs = meta.getTables(null, null, null,new String[] { "TABLE" });
		while (rs.next()) {
			t.tableToEntity(rs.getString("TABLE_NAME"));
		}
		conn.close();
	}
	
	/**
	 * 针对单个表的操作
	 */
	@Test
	public void tableToEntityByName(){
		t.tableToEntity("userlog");
	}
}
